﻿@model  Faurecia.WM.Web.Models.ScrapRecoredModel
<div class="page-bar">
    <ul class="page-breadcrumb">
        <li>
            <span id="module">佛吉亚管理</span>
            <i class="fa fa-angle-right"></i>
        </li>
        <li>
            <span id="action"></span>
        </li>
    </ul>
</div>
<!-- END PAGE BAR -->
<div class="mt-bootstrap-tables" style="margin-top: 20px">
    <div class="row">
        <div class="col-md-12">
            <div class="portlet light bordered">
                <div class="portlet-title">
                    <div class="caption">
                        <i class="icon-social-dribbble font-green hide"></i>
                    </div>
                    <div class="actions">
                        <a class="btn btn-default" href="@Url.Action("ScrapLog")">
                            <i class="fa fa-list-ul"></i> 报废日志
                        </a>
                    </div>
                </div>

                <div class="portlet-body form">
                    <!-- BEGIN FORM-->
                    <form id="defaultForm" class="form-horizontal" method="post" novalidate="novalidate">
                        <div class="form-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="CarNumber" class="control-label col-md-3">推车：</label>
                                        <div class="col-md-9">
                                            <select id="CarNumber" name="CarNumber" class="bs-select form-control" data-live-search="true" data-size="8" required>
                                                @if (ViewBag.CarList != null && ViewBag.CarList.Count > 0)
                                                {
                                                    foreach (var item in ViewBag.CarList)
                                                    {
                                                        <option value="@item.Number" productId="@item.ProductId" productCount="@item.ProductCount" areaId="@item.AreaId">@item.Number</option>
                                                    }
                                                }
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label col-md-3">产品：</label>
                                        <div class="col-md-9">
                                            <p class="form-control-static lblProductName">&nbsp;</p>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label col-md-3">
                                            <span class="required" aria-required="true">*</span>
                                            产品数：
                                        </label>
                                        <div class="col-md-9">
                                            <div class="input-icon right">
                                                <i class="fa"></i>
                                                @Html.TextBoxFor(x => x.ProductCount, new { type = "text", @class = "form-control", @vlaue="0", @maxlength = "7", @onkeyup = "inputInt(this)", @digits = true, @min = "0", })
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label col-md-3">
                                            类型：
                                        </label>
                                        <div class="col-md-9">
                                            <div class="input-icon right">
                                                <i class="fa"></i>
                                                <select id="ScrapType" name="ScrapType" class="form-control">
                                                    <option value="1">部份</option>
                                                    <option value="2">整车</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-actions">
                            <div class="row">
                                <div class="col-md-offset-3 col-md-9">
                                    <button type="submit" class="btn green">保存</button>

                                    <button type="reset" class="btn">重置</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <!-- END FORM-->
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script type="text/javascript">
        var page = function() {

            var handle1 = function() {
                var productList = @Html.Raw(ViewBag.ProductListJson);

                $('.bs-select').selectpicker({
                    iconBase: 'fa',
                    tickIcon: 'fa-check'
                });

                var getName = function(productId) {
                    var name = '该车无产品';
                    $.each(productList, function(index, data) {
                        if (data.Id === productId) {
                            name = data.FullName;
                            return false;
                        }
                    });
                    return name;
                };

                $('#CarNumber').on('hidden.bs.select', function() {
                    var selectCarNumber = $(this);
                    var val = selectCarNumber.selectpicker('val');
                    selectCarNumber.find("option[value='" + val + "']").attr("selected", true);
                    
                    var selectItem = $('#CarNumber option:selected');
                    var areaId = selectItem.attr('areaid');
                    var productId = selectItem.attr('productid');
                    var productCount = selectItem.attr('productcount');

                    $('.lblProductName').html(getName(productId));
                    $('#ProductCount').val(productCount);
                });
                
                customFormValidate($('#defaultForm'), {}, function(form) {
                    var data = $(form).serializeObject();

                    if (data.ProductCount == 0 && data.ScrapType == 1) {
                        showToast(3, '系统提示', '产品数不能为0');
                        return;
                    }

                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("SaveScrap")',
                        data: data,
                        async: false,
                        success: function(reslut) {
                            if (reslut.Success) {
                                showToast(0, '系统提示', '保存成功');
                            } else {
                                showToast(3, '系统提示', reslut.Message);
                                window.location.replace('scrap');
                            }
                        },
                        error: function(error) {
                            showToast(3, '系统提示', ' 操作失败!');
                        }
                    });
                });
            };

            return{
                init: function() {
                    handle1();
                }
            };
        }();

        jQuery(document).ready(function() {
            page.init();
        });
    </script>
}
